<?php

namespace App\Controller\Wap;

use App\Entity\Orders;
use App\Entity\ShopGoods;
use App\Entity\UserAddress;
use App\Entity\UserLikesProduct;
use App\Model\AreaModel;
use App\Model\CityModel;
use App\Model\MoneyWaitModel;
use App\Model\OrdersModel;
use App\Model\ProvinceModel;
use App\Model\ShopGoodsModel;
use App\Model\UserAddressModel;
use App\Model\UserLevelUpTaskModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseConfig;
use App\Base\BaseController4User;
use BaseExt\KdApi;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;


/**
 * @route("/systask")
 */
class SysTaskController extends BaseController4User
{

    private function initData()
    {
        return $this->data;
    }

    /**
     * @route("/levelup",name="TaskDealLevelUp")
     */
    public function TaskDealLevelUp()
    {
        $UserLevelUpTaskModel = new UserLevelUpTaskModel($this->getDoctrine());
        $list = $UserLevelUpTaskModel->getListWaiting();
        var_dump($list);
        foreach ($list as $row) {
            $Entity = $UserLevelUpTaskModel->getById($row["id"]);
            var_dump($Entity);
            if ($Entity) {
                $UserLevelUpTaskModel->dealWithUserLevelUp($Entity);
            }
        }
        die();
    }

    /**
     * @route("/moneywait",name="TaskMoneyWait")
     */
    public function TaskMoneyWait()
    {
        $UserLevelUpTaskModel = new MoneyWaitModel($this->getDoctrine());
        $list = $UserLevelUpTaskModel->getListWaiting();
        var_dump($list);
        foreach ($list as $row) {
            $Entity = $UserLevelUpTaskModel->getById($row["id"]);
            var_dump($Entity);
            if ($Entity) {
                $UserLevelUpTaskModel->dealWithMoneyWait($Entity);
            }
        }
        die();
    }

    /**
     * @route("/dealwithorder",name="dealwithorder")
     */
    public function dealwithorder()
    {
        $OrdersModel = new OrdersModel($this->getDoctrine());
        //超出7点的未支付订单删除出
        $rep = $OrdersModel->getRep();
        $rst = $rep->findBy(array(
            "status" => BaseConfig::__ORDER_CREATE__,
        ));
//        new Orders()
        foreach ($rst as $item) {
            /**@var $item Orders**/
            if ($item->getAddtime()->getTimestamp() + 7 * 24 * 3600 < time()) {
                $this->myEm()->remove($item);
            }
        }
        $this->myEm()->flush();
        //超出15天未收货的发货订单结束
        $rep = $OrdersModel->getRep();
        $rst = $rep->findBy(array(
            "status" =>  BaseConfig::__ORDER_DELIVER__,
        ));
        foreach ($rst as $item) {
            if ($item->getAddtime()->getTimestamp() + 15 * 24 * 3600 < time()) {
                $item->setStatus(BaseConfig::__ORDER_FINISH__);
                $this->myEm()->persist($item);
            }
        }
        $this->myEm()->flush();
        die();
    }










    /**
     * @route("/getOrderDeliverInfo",name="getOrderDeliverInfo")
     */
    public function getOrderDeliverInfo()
    {
        $api = new KdApi();
        $data = $api->getOrderTracesByJson("71384909329424","HTKY");
//        0-无轨迹
//        1-已揽收
//        2-在途中
//        3-签收
//        4-问题件
        return $this->json_out(1,"数据获取成功",json_decode($data,true));

    }

}
